package servlets;

import beans.StuInfoBean;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import services.StuService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class stuGetInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StuInfoBean stu = JSON.parseObject(request.getParameter("stuInfo"), new TypeReference<>() {});
        StuService stuService = new StuService();
        PrintWriter out = response.getWriter();
        if(stuService.insertStudent(stu) != 0)
            out.write("true");
        else
            out.write("false");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String searchType = request.getParameter("searchType");
        String search = request.getParameter("search");

        List<StuInfoBean> list;
        StuService stu = new StuService();
        HttpSession session = request.getSession(true);
        session.removeAttribute("stuList");
        session.removeAttribute("stuInfo");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        switch (searchType) {
            case "search_college":
                list = stu.findStudentsByCollege(search);
                if(! list.isEmpty()) {
                    session.setAttribute("stuList", list);
                    out.write(JSON.toJSONString(list));
                    //out.write("true");
                } else
                    out.write("false");
                break;
            case "search_major":
                list = stu.findStudentsByMajor(search);
                if(! list.isEmpty()) {
                    session.setAttribute("stuList", list);
                    out.write(JSON.toJSONString(list));
                    //out.write("true");
                } else
                    out.write("false");
                break;
            case "search_username":
                list = stu.findStudentsByName(search);
                if(! list.isEmpty()) {
                    session.setAttribute("stuList", list);
                    out.write(JSON.toJSONString(list));
                    //out.write("true");
                } else
                    out.write("false");
                break;
            case "search_id":
                StuInfoBean stuInfoBean;
                stuInfoBean = stu.findStudentById(search);
                if(stuInfoBean != null) {
                    session.setAttribute("stuInfo", stuInfoBean);
                    out.write(JSON.toJSONString(stuInfoBean));
                    //out.write("true");
                } else
                    out.write("false");
                break;
            case "searchAll":
                list = stu.findAllStudents();
                if(! list.isEmpty()) {
                    session.setAttribute("stuList", list);
                    out.write(JSON.toJSONString(list));
                    //out.write("true");
                } else
                    out.write("false");
                break;
            case "search_class":
                list = stu.findStudentsByClass(search);
                if(! list.isEmpty()) {
                    session.setAttribute("stuList", list);
                    out.write(JSON.toJSONString(list));
                    //out.write("true");
                } else
                    out.write("false");
                break;
            default:
                out.write("false");
                break;
        }

        System.out.println(searchType+"  "+search);
    }
}
